#!/usr/bin/python
# -*- coding: UTF-8 -*-
from aip import AipSpeech
import speech_recognition as sr
#import Sphinxbase
import pocketsphinx

#从百度AI开放平台创建应用处获取
APP_ID = '17307022'
API_KEY = 'Gp5H0e6fmvRQWKSk5aYgnViG'
SECRET_KEY = 'Hq7BVIpAuxijNP2GNSILHSBnF0oiCLNz '

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 读取文件
#def get_file_content(filePath):
#    with open(filePath, 'rb') as fp:
#        return fp.read()

# 识别本地文件
def get_text(audio):
    #result = client.asr(get_file_content('/home/pi/modules.ai/tmp.wave/record.wav'), 'wav', 16000, {
    result = client.asr(audio, 'wav', 16000, {
    'dev_pid': 1536,})
    print(result)
    text = result['result'][0]
    return text

# 进行离线语音识别
def get_text_offline(audio):
    r = sr.Recognizer()
    print(r)
    try:
        text = r.recognize_sphinx(audio)
        print(text)
        return text
    except sr.UnknownValueError:
        print("Sphinx could not understand audio")
    except sr.RequestError as e:
        print("Sphinx error; {0}".format(e))

if __name__ == '__main__':
    get_text()
    print(get_text())
